package com.wyp168.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyp168.commonutils.R;
import com.wyp168.commonutils.StatusCode;
import com.wyp168.servicebase.exceptionhandler.GuliException;
import com.wyp168.serviceedu.entity.EduTeacher;
import com.wyp168.serviceedu.entity.vo.TeacherQuery;
import com.wyp168.serviceedu.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author wyp168
 * @since 2022-04-26
 */
@Api(tags = "讲师控制器")
@RestController
@CrossOrigin
@RequestMapping("/serviceedu/edu-teacher")
public class EduTeacherController {
    //把service注入
    @Autowired
    private EduTeacherService teacherService;

    //rest 风格
    @ApiOperation("所有讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher() {
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    @ApiOperation("逻辑删除指定id的讲师记录")
    @DeleteMapping("/delete/{id}")
    public R deleteLogicTeacher(
            @ApiParam("需要删除的教师的id") @PathVariable("id") String id) {
        boolean isDelete = teacherService.removeById(id);
        if (isDelete) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    /**
     * 分页查询讲师
     */
    @ApiOperation("分页查询讲师信息")
    @GetMapping("pageTeacher/{pageCurrent}/{pageSize}")
    public R pageListTeacher(
            @ApiParam("页码数") @PathVariable("pageCurrent") Integer pageCurrent,
            @ApiParam("每页显示记录数") @PathVariable("pageSize") Integer pageSize
    ) {
        //创建Page对象，接收pageCurrent和pageSize参数
        IPage<EduTeacher> pageTeacher = new Page<>(pageCurrent,pageSize);
        //调用方法实现分页
        //嗲用方法时，底层封装，把分页所有数据都封装到teacherPage对象中
        teacherService.page(pageTeacher,null);

        long total = pageTeacher.getTotal();    //总记录数
        List<EduTeacher> records = pageTeacher.getRecords();    //记录list集合

        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 多条件组合查询带分页
     */
    @ApiOperation("多条件组合查询带分页")
    @PostMapping("pageConditioin/{currentPage}/{pageSize}")
    public R pageTeacherCondition(
            @ApiParam("当前页") @PathVariable long currentPage,
            @ApiParam("记录数") @PathVariable long pageSize,
            @ApiParam("封装条件的对象") @RequestBody(required = false) TeacherQuery teacherQuery
    ) {
        //创建Page对象
        IPage<EduTeacher> pageTeacher = new Page<>(currentPage,pageSize);
        //构建条件
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        //判断条件值是否为空，如果不为空，则拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //构建条件
        if(!StringUtils.isEmpty(name)) {
            queryWrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_modified",end);
        }

        //以修改时间倒序排序
        queryWrapper.orderByDesc("gmt_modified");

        //多条件组合查询并分页
        teacherService.page(pageTeacher,queryWrapper);

        long total = pageTeacher.getTotal();    //总记录数

        List<EduTeacher> records = pageTeacher.getRecords();    //记录list集合

        return R.ok().data("total",total).data("rows",records).data("total",total);
    }

    //添加讲师的接口方法
    @ApiOperation("添加讲师接口")
    @PostMapping("addTeacher")
    public R addTeacher(
            @ApiParam("前端传递过来的讲师数据对象") @RequestBody EduTeacher eduTeacher
    ) {
        boolean isSave = teacherService.save(eduTeacher);
        if(isSave) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //根据id查询讲师信息
    @ApiOperation("根据id查询讲师信息")
    @GetMapping("getTeacherById/{id}")
    public R getTeacher(
            @ApiParam("查询的讲师id") @PathVariable String id
    ) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        EduTeacher eduTeacher = teacherService.getById(id);
        System.out.println(eduTeacher);
        R result = R.ok().data("teacher", eduTeacher);
        System.out.println(result);
        return result;
    }

    //讲师修改功能
    @ApiOperation("根据id修改讲师信息")
    @PostMapping("updateTeacher")
    public R updateTeacher(
            @ApiParam("需要修改为的讲师数据") @RequestBody(required = false) EduTeacher eduTeacher
    ) {
        boolean isUpdate = teacherService.updateById(eduTeacher);
        if (isUpdate) {
            return R.ok();
        } else{
            return R.error();
        }
    }
}

